package com.example.pingze.controller;

import com.example.pingze.entity.Comment;
import com.example.pingze.entity.CommentUserLike;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.CommentService;
import com.example.pingze.service.CommentUserLikeService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@RestController
@RequestMapping("/commentUserLikes")
@CrossOrigin(origins = "*")
public class CommentUserLikeController {
    @Autowired
    private CommentUserLikeService service;
    @Autowired
    private CommentService commentService;
    //点赞
    @PostMapping("/likes")
    public Result likes(@RequestParam String commentId,
                        @RequestParam String account) {
        if (commentId == null) {
            throw new PingZeException("commentUserLike_评论ID为空");
        }
        if (account == null) {
            throw new PingZeException("commentUserLike_评论account为空");
        }
        //在存点赞信息之前需要验证，该条点赞信息在数据库是否已经存在
        CommentUserLike commentUserLike = service.findCommentUserLikeByAccountAndCommentId(account, commentId);
        if (commentUserLike == null) {
            CommentUserLike commentUserLike1 = new CommentUserLike();
            commentUserLike1.setId(UUID.randomUUID().toString());
            commentUserLike1.setCommentId(commentId);
            commentUserLike1.setAccount(account);
            service.saveCommentUserLike(commentUserLike1);
            Comment comment = commentService.findCommentById(commentId);
            if (comment != null) {
                int likes = comment.getLikes();
                int count = likes + 1;
                comment.setLikes(count);
                commentService.saveComment(comment);
            }
            return ResultVOUtils.result(comment.getLikes());
        }else {
            throw new PingZeException("commentUserLike_已经为该评论点赞，无需二次操作");
        }
    }
    //取消点赞
    @PostMapping("/noLikes")
    public Result<String> noLove(@RequestParam String account,
                                 @RequestParam String commentId){
        if (commentId == null){
            throw new PingZeException("commentUserLike_点赞评论id为空");
        }
        if(account == null){
            throw new PingZeException("commentUserLike_账号为空");
        }
        CommentUserLike commentUserLike = service.findCommentUserLikeByAccountAndCommentId(account, commentId);
        if (commentUserLike == null) {
            throw new PingZeException("该评论信息不在点赞列表中");
        }
        Comment comment = commentService.findCommentById(commentId);
        if (comment != null) {
            int i = comment.getLikes();
            int num = i - 1;
            comment.setLikes(num);
            commentService.saveComment(comment);
            service.delete(commentUserLike);
            return ResultVOUtils.result(num);
        }else {
            throw new PingZeException("请先对该评论进行点赞");
        }
    }
    //获取点赞状态
    @PostMapping("/getStatus")
    public Result<String> getStatus(@RequestParam String account,
                                    @RequestParam String commentId){
        String status = "";
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(commentId == null){
            throw new PingZeException("评论ID为空");
        }
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        CommentUserLike commentUserLike = service.findCommentUserLikeByAccountAndCommentId(account, commentId);
        System.out.println(commentUserLike);
        if(commentUserLike != null){
            status = "ok";
        }else{
            status = "no";
        }
        return ResultVOUtils.success(status);
    }
}
